package com.wangxinrou.wangxinrou_surprisemeal.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangxinrou.wangxinrou_surprisemeal.mapper.RestaurantMapper;
import com.wangxinrou.wangxinrou_surprisemeal.model.Restaurant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RestaurantService {

    @Autowired
    private RestaurantMapper restaurantMapper;

    // 添加餐馆
    public int addRestaurant(Restaurant restaurant) {
        return restaurantMapper.insert(restaurant);
    }

    // 获取所有餐馆
    public List<Restaurant> getAllRestaurants() {
        return restaurantMapper.selectList(null);
    }

    // 根据 ID 获取餐馆
    public Restaurant getRestaurantById(Integer id) {
        return restaurantMapper.selectById(id);
    }

    // 更新餐馆信息
    public int updateRestaurant(Restaurant restaurant) {
        return restaurantMapper.updateById(restaurant);
    }

    // 根据 ID 删除餐馆
    public int deleteRestaurantById(Integer id) {
        return restaurantMapper.deleteById(id);
    }

    // 批量添加餐馆
    public int addRestaurants(List<Restaurant> restaurants) {
        return restaurantMapper.insertBatchSomeColumn(restaurants);
    }

    // 清空所有餐厅记录的方法
    public void clearAllRestaurants() {
        restaurantMapper.delete(null);
    }

    // 查询位置为特定值的餐馆
    public List<Restaurant> findByLocation(String location) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address", location);
        return restaurantMapper.selectList(queryWrapper);
    }

    // 查询评分大于特定值的餐馆
    public List<Restaurant> findByRatingGreaterThan(Double rating) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("rating", rating);
        return restaurantMapper.selectList(queryWrapper);
    }

    // 模糊查询名称包含特定字符串的餐馆
    public List<Restaurant> findByNameLike(String name) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return restaurantMapper.selectList(queryWrapper);
    }

    // 查询评分在两个值之间的餐馆
    public List<Restaurant> findByRatingBetween(Double minRating, Double maxRating) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("rating", minRating, maxRating);
        return restaurantMapper.selectList(queryWrapper);
    }

    // 复合查询：位置和评分
    public List<Restaurant> findByLocationAndRating(String location, Double rating) {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("address", location).gt("rating", rating);
        return restaurantMapper.selectList(queryWrapper);
    }

    // 按评分降序排列
    public List<Restaurant> findSortedByRating() {
        QueryWrapper<Restaurant> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("rating");
        return restaurantMapper.selectList(queryWrapper);
    }

    //启用分页查询
    public List<Restaurant> getRestaurants(int pageNum, int pageSize) {
        // 创建 Page 对象
        IPage<Restaurant> page = new Page<>(pageNum, pageSize);  // 第 pageNum 页，每页 pageSize 条
        // 执行分页查询
        IPage<Restaurant> restaurantPage = restaurantMapper.selectPage(page, null);
        // 获取当前页的餐馆记录
        return restaurantPage.getRecords();
    }
}